import 'dart:io';

import 'package:permission_handler/permission_handler.dart';
import 'package:share_fit/app/controllers/base/base_controller.dart';
import 'package:share_fit/app/controllers/device/call_remind/call_const.dart';
import 'package:share_fit/app/helper/ext/tr_ext.dart';
import 'package:share_fit/app/tools/log_utils.dart';
import 'package:share_fit/app/ute/uteped_const.dart';
import 'package:share_fit/app/ute/uteped_device.dart';
import 'package:share_fit/generated/locales.g.dart';

import 'call_remind_state.dart';

class CallRemindLogic extends BaseController {
  final CallRemindState state = CallRemindState();

  @override
  void onInit() {
    super.onInit();
    UTEPedDevice.instance.getUteConfig().checkBT3();
    // _getFindWearState();
  }

  @override
  Future<void> onReady() async {
    _checkPermission();
    super.onReady();
  }

  Future<bool> _checkPermission() async {
    var granted = true;
    if (Platform.isAndroid) {
       granted = await Permission.contacts.isGranted;
      if (!granted) {
        granted = await Permission.contacts.request().isGranted;
        if (!granted) {
          showPermissionDialog(LocaleKeys.contact_permission_tips.ts);
        }
      }

      granted = await Permission.phone.isGranted;
      if (!granted) {
        granted = await Permission.phone.request().isGranted;
        if (!granted) {
          showPermissionDialog(LocaleKeys.phone_permission_tips.ts);
        }
      }
    }

    return granted;
  }

  Future<void> setCallRemindEnable(bool value) async {
    if (!await _checkPermission()) {
      return;
    }
    showLoading();
    state.callRemindEnable = value;
    bRemoteReqAsync(CallConst.methodSetCallRemindEnable, {UTEPedConst.keyEnable: value})
        .then((success) => _onSetCallRemindEnable(success, value));
  }

  void _onSetCallRemindEnable(success, value) {
    cancelLoading();
    if (success) {
      save(CallConst.methodSetCallRemindEnable, value);
    }
    else {
      state.callRemindEnable = !value;
    }
    update([CallConst.keyCallRemindEnable]);
  }

  void setCallRemindHangup(bool value) {
    showLoading();
    state.callRemindHangup = value;
    bRemoteReqAsync(CallConst.methodSetCallRemindHangup, {UTEPedConst.keyEnable: value})
        .then((success) => _onSetCallRemindHangup(success, value));
    logger.d("over");
  }

  void _onSetCallRemindHangup(success, value) {
    cancelLoading();
    if (success) {
      save(CallConst.methodSetCallRemindHangup, value);
    }
    else {
      state.callRemindHangup = !value;
    }
    update([CallConst.keyCallRemindHangup]);
  }

  void setCallRing(String id, bool value) {
    var wearCmd = state.wearState;
    switch(id) {
      case CallConst.keyCallRingRing:
        state.callRingRing = value;
        state.callRingVibrate = false;
        state.callRingRingVibrate = false;
        state.callRingMute = false;
        break;
      case CallConst.keyCallRingVibrate:
        state.callRingRing = false;
        state.callRingVibrate = value;
        state.callRingRingVibrate = false;
        state.callRingMute = false;
        break;
      case CallConst.keyCallRingRingVibrate:
        state.callRingRing = false;
        state.callRingVibrate = false;
        state.callRingRingVibrate = value;
        state.callRingMute = false;
        break;
      case CallConst.keyCallRingMute:
        state.callRingRing = false;
        state.callRingVibrate = false;
        state.callRingRingVibrate = false;
        state.callRingMute = value;
        break;
    }
    update([CallConst.keyCallRingRing, CallConst.keyCallRingVibrate,
      CallConst.keyCallRingRingVibrate, CallConst.keyCallRingMute, CallConst.keyCallRingText]);

    // bRemoteReq(CallConst.methodSetFindWearCmd, {UTEPedConst.keyEnable: value})
    //     .then((success) => {});
    _saveStorage(id, value);
  }

  void _getFindWearState() {
    dRemoteReqAsync(CallConst.methodGetFindWearState, {})
        .then((result) => logger.d(result));
  }

  void _saveStorage(String id, bool value) {
    save(CallConst.keyCallRingRing,
        id == CallConst.keyCallRingRing ? value : false);
    save(CallConst.keyCallRingVibrate,
        id == CallConst.keyCallRingVibrate ? value : false);
    save(CallConst.keyCallRingRingVibrate,
        id == CallConst.keyCallRingRingVibrate ? value : false);
    save(CallConst.keyCallRingMute,
        id == CallConst.keyCallRingMute ? value : false);
  }

  bool switchCallRing(String id) {
    bool result = false;
    switch(id) {
      case CallConst.keyCallRingRing:
        result = state.callRingRing;
        break;
      case CallConst.keyCallRingVibrate:
        result = state.callRingVibrate;
        break;
      case CallConst.keyCallRingRingVibrate:
        result = state.callRingRingVibrate;
        break;
      case CallConst.keyCallRingMute:
        result = state.callRingMute;
        break;
    }
    return result;
  }

  String getCallRing() {
    if(state.callRingRing) {
      return LocaleKeys.call_ringing_ring.ts;
    }
    if(state.callRingVibrate) {
      return LocaleKeys.call_ringing_vibrate.ts;
    }
    if(state.callRingRingVibrate) {
      return LocaleKeys.call_ringing_ring_vibrate.ts;
    }
    if(state.callRingMute) {
      return LocaleKeys.call_ringing_mute.ts;
    }
    return '';
  }

  @override
  String getRemotePath() {
    return UTEPedConst.pathContact;
  }

  @override
  String getStoragePath() {
    return UTEPedConst.pathDevice;
  }

  @override
  Future<void> initState() async {
    state.callRemindHangup = read(CallConst.methodSetCallRemindHangup, defaultValue: false);
    state.callRemindEnable = read(CallConst.methodSetCallRemindEnable, defaultValue: false);
    if (Platform.isAndroid) {
      var granted = await Permission.contacts.isGranted;
      if (granted) {
        granted = await Permission.phone.isGranted;
      }
      if (!granted) {
        state.callRemindEnable = false;
      }
    }
  }

  @override
  void onRemoteEvent(event, method) {
    if (method == UTEPedConst.methodCheckPermissions) {
      var success = event[UTEPedConst.keyResult];
      var type = event[UTEPedConst.keyValue];
      if (success && type == UTEPedConst.permissionCall) {
        state.callRemindEnable = true;
        _onSetCallRemindEnable(success, true);
      }
    }
  }
}
